package com.zhongxi.guoyuan.view.user;

import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.gson.Gson;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.application.MyApplication;
import com.zhongxi.guoyuan.base.BaseActivity;
import com.zhongxi.guoyuan.entity.BaseResponse;
import com.zhongxi.guoyuan.url.Url;
import com.zhongxi.guoyuan.view.MainHomeActivity;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class SetPasswordActivity extends BaseActivity implements View.OnClickListener {

    private Button btSure;
    private EditText etPassword;

    @Override
    protected void initView() {
        super.initView();
        setContentView(R.layout.activity_set_password);
        initTitle();
        tvTitle.setText("设置密码");
        btSure = (Button) findViewById(R.id.btSure);
        etPassword = (EditText) findViewById(R.id.etPassword);
    }

    @Override
    protected void initData() {
        super.initData();
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        ivBack.setOnClickListener(this);
        btSure.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ivBack:
                finish();
                break;
            case R.id.btSure:
                if(TextUtils.isEmpty(etPassword.getText().toString().trim())){
                    showShortToast("密码不能为空");
                }else {
                    Map<String, String> map = new HashMap<String, String>();
                    map.clear();
                    map.put("useid", MyApplication.getInstance().getUseId());
                    map.put("newpwd", MyApplication.getInstance().descsc(etPassword.getText().toString().trim()));
                    map.put("oldpwd","");
                    getServer(Url.SET_PWD, map, "", false, null, "post");
                }
                break;
        }
    }

    @Override
    protected void processSuccessResult(String response) {
        super.processSuccessResult(response);
        Gson gson = new Gson();
        try{
            BaseResponse result = gson.fromJson(response, BaseResponse.class);
            if(null!=result.getSafeobj()&&!"".equals(result.getSafeobj())){
                MyApplication.getInstance().setSpValue(result.getSafeobj());
            }
            Toast.makeText(this, "设置成功", Toast.LENGTH_LONG).show();
//            startAct(LoginActivity.class);
            startAct(MainHomeActivity.class);
            finish();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    protected void ProcessFailResult(String response) {
        super.ProcessFailResult(response);
        Gson gson = new Gson();
        BaseResponse result = gson.fromJson(response, BaseResponse.class);
        if(null!=result.getSafeobj()&&!"".equals(result.getSafeobj()))
            MyApplication.getInstance().setSpValue(result.getSafeobj());
        try{
            JSONObject obj = new JSONObject(response);
            int errorcode = obj.getInt("code");
            if(errorcode == 1000){
//                showShortToast("参数错误");
            }else if(errorcode == 7003){
                showShortToast("旧密码错误");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
